<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="logo">
        <el-icon><Building /></el-icon>
        <span class="logo-text">物业管理系统</span>
      </div>
      <div class="header-right">
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-icon><Bell /></el-icon>
            <span class="notification-badge">3</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>新消息 1</el-dropdown-item>
              <el-dropdown-item>新消息 2</el-dropdown-item>
              <el-dropdown-item>新消息 3</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-icon><Setting /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人设置</el-dropdown-item>
              <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <div class="user-info">
          <el-avatar :size="40">{{ currentUsername.slice(0, 1) }}</el-avatar>
          <span class="user-name">{{ currentUsername }}</span>
        </div>
      </div>
    </header>

    <!-- 主要内容区 -->
    <div class="main-content">
      <!-- 侧边菜单 -->
      <aside class="sidebar">
        <el-menu
          :default-active="activePath"
          class="sidebar-menu"
          router
          background-color="transparent"
          text-color="#fff"
          active-text-color="#ffd04b"
        >
          <el-menu-item index="/">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/property">
            <el-icon><OfficeBuilding /></el-icon>
            <span>房产管理</span>
          </el-menu-item>
          <el-menu-item index="/repair">
            <el-icon><Tools /></el-icon>
            <span>维修管理</span>
          </el-menu-item>
          <el-menu-item index="/fee">
            <el-icon><WalletFilled /></el-icon>
            <span>费用管理</span>
          </el-menu-item>
          <el-menu-item index="/user">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
        </el-menu>
      </aside>

      <!-- 内容区 -->
      <main class="content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<script setup>
import { computed, ref, onMounted } from 'vue'
import { 
  HomeFilled, Building, OfficeBuilding, Tools, WalletFilled, User, 
  Setting, Bell 
} from '@element-plus/icons-vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

// 计算当前激活的路由路径
const route = useRoute()
const router = useRouter()
const activePath = computed(() => {
  return route.path
})

// 当前用户名
const currentUsername = ref('管理员')

// 组件挂载时获取用户名
onMounted(() => {
  const username = localStorage.getItem('username') || sessionStorage.getItem('username')
  if (username) {
    currentUsername.value = username
  }
})

// 处理退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '退出确认', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    // 清除登录状态
    localStorage.removeItem('isLoggedIn')
    localStorage.removeItem('username')
    sessionStorage.removeItem('isLoggedIn')
    sessionStorage.removeItem('username')
    
    ElMessage.success('已成功退出登录')
    
    // 重定向到登录页
    router.push('/login')
  } catch (error) {
    // 用户取消退出
    ElMessage.info('已取消退出')
  }
}
</script>

<script>
export default {
  name: 'AppLayout'
}
</script>

<style scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.header {
  height: 60px;
  background-image: url('../assets/building-bg.svg');
  background-size: cover;
  background-position: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
}

.logo-text {
  margin-left: 10px;
  color: #ffffff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
  font-weight: bold;
}

.header-right {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
  margin-right: 20px;
  color: #ffffff;
  position: relative;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.notification-badge {
  position: absolute;
  top: -5px;
  right: -10px;
  background-color: #f56c6c;
  color: #fff;
  font-size: 10px;
  padding: 2px 5px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
  text-shadow: none;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-name {
  margin-left: 10px;
  color: #ffffff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.main-content {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.sidebar {
  width: 200px;
  background-image: url('../assets/building-bg.svg');
  background-size: cover;
  background-position: center;
  background-color: #545c64;
  box-shadow: 2px 0 4px rgba(0, 0, 0, 0.2);
}

.sidebar-menu {
  height: 100%;
  border-right: none;
}

.content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  background-attachment: fixed;
}
</style>